package utils.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.PersonalDPEntity;
import entity.PersonalDetailsEntity;
import entity.PersonalSoundEntity;
import tools.ItemOnClickListenerOther;
import utils.roundimage.RoundNetWorkImageView;
import tools.TimeUtil;
import utils.texttools.StringUtil;
import volley.VolleySingleton;

/**
 * Created by dllo on 15/11/24.
 */
public class PersonalDetailsAdapter extends
        RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    private static final int TYPE00 = 0;
    private static final int TYPE01 = 1;
    private static final int TYPE02 = 2;
    private Context context;
    private LayoutInflater inflater;
    private int itemPosition;
    private int posi;
    private PersonalDetailsEntity deentity;
    private PersonalDPEntity dpentity;
    private ItemOnClickListenerOther onClickListenerOther,sound;
//    private ItemOnClickListener sound;



    private List<PersonalSoundEntity.ListEntity> entities;

    public void setLinstener(ItemOnClickListenerOther back) {
        onClickListenerOther = back;
    }



    public PersonalDetailsAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);

    }

    public void addSeList(List<PersonalSoundEntity.ListEntity> entities) {
        this.entities = entities;
        notifyDataSetChanged();
    }

    public void addDpdata(PersonalDPEntity dpentity) {
        this.dpentity = dpentity;
        notifyDataSetChanged();
    }

    public void addDedata(PersonalDetailsEntity deentity) {
        this.deentity = deentity;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE00:
                return new ViewHolder00(inflater.inflate(R.layout.fragment_details_personal_item_o, parent, false));
            case TYPE01:
                return new ViewHolder01(inflater.inflate(R.layout.fragment_details_personal_item_t, parent, false));
            case TYPE02:
                return new ViewHolder02(inflater.inflate(R.layout.fragment_details_personal_item_th, parent, false));
        }
        return null;
    }



    private boolean isListNull() {
        return entities != null && entities.size() > 0;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (deentity != null) {
            if (holder instanceof ViewHolder00) {
                final ViewHolder00 h00 = (ViewHolder00) holder;
                VolleySingleton.getInstance().getBitmap(deentity.getMobileLargeLogo(), h00.userPhoto);

                VolleySingleton.getInstance().getBitmap(deentity.getBackgroundLogo(), h00.personalDebg);
                h00.userName.setText(deentity.getNickname());
                h00.userSignature.setText(deentity.getPersonalSignature());
                h00.personalGzTv.setText(StringUtil.longToWan(deentity.getFollowings()));
                h00.persomalFsTv.setText(StringUtil.longToWan(deentity.getFollowers()));
                h00.personalZanTv.setText(StringUtil.longToWan(deentity.getFavorites()));
                h00.pos = position;
                h00.personalDebg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context, "13", Toast.LENGTH_SHORT).show();
                        Animation animation = AnimationUtils.loadAnimation(context,
                                R.anim.personal_img_translate);
                        animation.setFillAfter(true);
                        h00.userPhoto.startAnimation(animation);
                        Animation animation1 = AnimationUtils.loadAnimation(context,
                                R.anim.personal_btn_rotate);
                        animation1.setFillAfter(true);
                        h00.personalDownBtn.startAnimation(animation1);

                    }
                });
            }


            if (dpentity != null) {
                if (holder instanceof ViewHolder01) {
                    ViewHolder01 h01 = (ViewHolder01) holder;
                    VolleySingleton.getInstance().getBitmap(dpentity.getList().get(0).getCoverLarge(), h01.personalDeImg);
                    h01.personalTtitle.setText("(" + dpentity.getTotalCount() + ")");
                    h01.personalDeTitle.setText(dpentity.getList().get(0).getTitle());
                    h01.personalDeContent.setText("最后更新 " + TimeUtil.getDescriptionTimeFromTimestamp(dpentity.getList().get(0).getUpdatedAt()));
                    h01.personalDePlayNum.setText(StringUtil.longToWan(dpentity.getList().get(0).getPlayTimes()));
                    h01.personalDeTracks.setText(dpentity.getList().get(0).getTracks() + "集");
                    h01.personalDeMore.setOnClickListener(this);
                    h01.personalDeInfor.setOnClickListener(this);
                    itemPosition = position;
                }

                if (isListNull()) {
                    if (holder instanceof ViewHolder02) {
                        PersonalSoundEntity.ListEntity entity = entities.get(position - 2);
                        ViewHolder02 h02 = (ViewHolder02) holder;

                        VolleySingleton.getInstance().getBitmap(entity.getSmallLogo(), h02.personalSoundImg);
                        h02.personalSoundTitle.setText(entity.getTitle());
                        h02.personalSoundContent.setText("by " + entity.getNickname());
                        h02.personalSoundPlay.setText(StringUtil.longToWan(entity.getPlaytimes()));
                        h02.personalSoundltime.setText(StringUtil.longToWan(entity.getDownloadSize()));
                        h02.personalSoundTime.setText(TimeUtil.getDescriptionTimeFromTimestamp(entity.getCreatedAt()));
                        h02.mPosition = position;

                    }
                }
            }
        }
    }

    @Override
    public int getItemCount() {

        return isListNull() ? (entities.size() + 2) : 2;
    }


    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return TYPE00;

        if (position == 1)
            return TYPE01;

        return TYPE02;
    }

    //发布的专辑
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.personalDeInfor:
                Toast.makeText(context, "详情" + itemPosition, Toast.LENGTH_SHORT).show();
                onClickListenerOther.itemListener(dpentity,4);
                break;
            case R.id.personalDeMore:
                Toast.makeText(context, "更多" + itemPosition, Toast.LENGTH_SHORT).show();
                break;
        }
    }


    public class ViewHolder00 extends RecyclerView.ViewHolder implements View.OnClickListener {
        private RoundNetWorkImageView userPhoto;
        private TextView userName, userSignature, personalGzTv, persomalFsTv, personalZanTv;
        private NetworkImageView personalDebg;
        private ImageView personalDownBtn, backIv;
        private Button focusonBtn;
        private int pos;
        public ViewHolder00(View itemView) {
            super(itemView);
            backIv = (ImageView) itemView.findViewById(R.id.backIv);
            backIv.setOnClickListener(this);
            focusonBtn = (Button) itemView.findViewById(R.id.focusonBtn);
            focusonBtn.setOnClickListener(this);
            userPhoto = (RoundNetWorkImageView) itemView.findViewById(R.id.userPhoto);
            userName = (TextView) itemView.findViewById(R.id.userName);
            userSignature = (TextView) itemView.findViewById(R.id.userSignature);
            personalGzTv = (TextView) itemView.findViewById(R.id.personalGzTv);
            persomalFsTv = (TextView) itemView.findViewById(R.id.persomalFsTv);
            personalZanTv = (TextView) itemView.findViewById(R.id.personalZanTv);
            personalDebg = (NetworkImageView) itemView.findViewById(R.id.personalDebg);
            personalDownBtn = (ImageView) itemView.findViewById(R.id.personalDownBtn);
//            personalDebg.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Animation animation = AnimationUtils.loadAnimation(context,
//                            R.anim.personal_img_rotate);
//                    userPhoto.setAnimation(animation);
//
//                }
//            });
        }

        //上面布局的监听
        @Override
        public void onClick(View v) {
//            PersonalSoundEntity.ListEntity entiti = entities.get(pos);
            if (onClickListenerOther != null) {
                switch (v.getId()) {
                    case R.id.backIv://点击头像上面的返回图标返回
                        onClickListenerOther.itemListener(deentity, 1);
                        Toast.makeText(context, "返回", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.focusonBtn://加关注的监听
                        onClickListenerOther.itemListener(deentity, 2);
                        break;
                }
            }

        }
    }

    public class ViewHolder01 extends RecyclerView.ViewHolder {
        private TextView personalTtitle, personalDeTitle, personalDeContent, personalDePlayNum, personalDeTracks;
        private NetworkImageView personalDeImg;
        private LinearLayout personalDeMore, personalDeInfor;

        public ViewHolder01(View itemView) {
            super(itemView);
            personalDeImg = (NetworkImageView) itemView.findViewById(R.id.personalDeImg);
            personalTtitle = (TextView) itemView.findViewById(R.id.personalTtitle);
            personalDeTitle = (TextView) itemView.findViewById(R.id.personalDeTitle);
            personalDeContent = (TextView) itemView.findViewById(R.id.personalDeContent);
            personalDePlayNum = (TextView) itemView.findViewById(R.id.personalDePlayNum);
            personalDeTracks = (TextView) itemView.findViewById(R.id.personalDeTracks);
            personalDeMore = (LinearLayout) itemView.findViewById(R.id.personalDeMore);
            personalDeInfor = (LinearLayout) itemView.findViewById(R.id.personalDeInfor);

        }
    }

    public class ViewHolder02 extends RecyclerView.ViewHolder implements View.OnClickListener {
        private RoundNetWorkImageView personalSoundImg;
        private TextView personalSoundTitle, personalSoundContent,
                personalSoundPlay, personalSoundltime, personalSoundTime;
        private ImageView personalSoundDowload;
        private LinearLayout personalSoundInfor;
        private int mPosition;
        private RelativeLayout itemPersonal;

        public ViewHolder02(View itemView) {
            super(itemView);
            personalSoundImg = (RoundNetWorkImageView) itemView.findViewById(R.id.personalSoundImg);
            personalSoundTitle = (TextView) itemView.findViewById(R.id.personalSoundTitle);
            personalSoundContent = (TextView) itemView.findViewById(R.id.personalSoundContent);
            personalSoundPlay = (TextView) itemView.findViewById(R.id.personalSoundPlay);
            personalSoundltime = (TextView) itemView.findViewById(R.id.personalSoundltime);
            personalSoundTime = (TextView) itemView.findViewById(R.id.personalSoundTime);
            personalSoundDowload = (ImageView) itemView.findViewById(R.id.personalSoundDowload);
            personalSoundInfor = (LinearLayout) itemView.findViewById(R.id.personalSoundInfor);
            personalSoundInfor.setOnClickListener(this);
            personalSoundDowload.setOnClickListener(this);
            itemPersonal = (RelativeLayout) itemView.findViewById(R.id.itemPersonal);
            itemPersonal.setOnClickListener(this);

        }


        //发布的声音监听事件
        @Override
        public void onClick(View v) {
            PersonalSoundEntity.ListEntity e = entities.get(mPosition);
            if (onClickListenerOther!=null){
                switch (v.getId()) {
                    case R.id.personalSoundDowload:
                        onClickListenerOther.itemListener(e,3);
                        //   Toast.makeText(context, "下载", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.itemPersonal:
                        onClickListenerOther.itemListener(e,5);
                        break;
                }
            }

        }
    }

}
